package com.qdxwx.data.service.shopcart;

import com.qdxwx.common.page.PageRequest;
import com.qdxwx.common.utils.ObjectUtils;
import com.qdxwx.data.mapper.shopcart.ShopCartMapper;
import com.qdxwx.data.service.course.CourseService;
import com.qdxwx.data.service.teacher.TeacherService;
import com.qdxwx.models.course.CourseDTO;
import com.qdxwx.models.shopcart.ShopCart;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;


/**
 * Qdx NetSchool Platform
 * @author qdxwx
 * @updateTime 2022/5/25
 * @description
 */
@Service
public class ShopCartServiceImpl implements ShopCartService {

	private final Logger logger = LoggerFactory.getLogger(ShopCartServiceImpl.class);

	@Autowired
	private ShopCartMapper shopCartMapper;
	@Autowired
	private CourseService courseService;
	@Autowired
	private TeacherService teacherService;

	/**
	 * 添加ShopCart
	 *
	 * @return id
	 */
	@Override
	public int insert(Integer goodsId, Integer type, Long userId) {
		ShopCart shopcart = new ShopCart();
		shopcart.setGoodsId(goodsId);
		shopcart.setTypeId(type);
		shopcart.setUserId(userId);
		shopcart.setAddTime(LocalDateTime.now());
		if (isExist(shopcart)) {
			return 0;
		}
		//添加到购物车中
		return shopCartMapper.insert(shopcart);
	}

	/**
	 * 根据id删除一个ShopCart
	 */
	@Override
	public void deleteById(Long id, Long userId) {
		shopCartMapper.deleteById(id, userId);
		// 移除缓存中的购物车数据
		/*
		List<ShopCart> list = (List<ShopCart>) CacheUtils.get(CacheConstants.SHOPCART_PREFIX + userId);
		if (list == null) return;
		for (ShopCart shopcart : list) {
			if (Objects.equals(shopcart.getId(), id)) {
				list.remove(shopcart);
				break;
			}
		}
		CacheUtils.set(CacheConstants.SHOPCART_PREFIX + userId, list);*/
	}

	@Override
	public List<ShopCart> getList(ShopCart shopcart, PageRequest page) {
		return shopCartMapper.selectList(shopcart, page);
	}

	/**
	 * 根据条件获取ShopCart列表
	 */
	@Override
	public List<ShopCart> getListWithCourse(long userId, int type) {
		List<ShopCart> list = getListByUserId(userId, type);
		if (ObjectUtils.isNotNull(list)) {
			// 将讲师的list放到旧的list中
			for (ShopCart shopcart : list) {
				CourseDTO course = courseService.getDTO(shopcart.getGoodsId());
				if (course != null) {
					course.setTeacherList(teacherService.getListByCourseId(course.getCourseId()));
					shopcart.setCourse(course);
				}
			}
		} else {
			list = new ArrayList<>();
		}
		return list;
	}

	@Override
	public List<ShopCart> getListByUserId(long userId, int typeId) {
		ShopCart query = new ShopCart();
		query.setTypeId(typeId);
		query.setUserId(userId);
		return shopCartMapper.selectList(query, null);
	}

	@Override
	public boolean isExist(ShopCart query) {
		List<ShopCart> list = shopCartMapper.selectList(query, new PageRequest(1, 1));
		return list != null && list.size() > 0;
	}

	@Override
	public boolean isExist(long userId, int typeId,int goodsId) {
		ShopCart query = new ShopCart();
		query.setGoodsId(goodsId);
		query.setTypeId(typeId);
		query.setUserId(userId);
		return isExist(query);
	}

	/**
	 * 清空数据库的购物车
	 */
	@Override
	public void deleteByUserId(long userId, int type) {
		shopCartMapper.deleteByUserId(userId, type);
	}
}